package controller

import (
	"gitee.com/song-yao-shun/MQH/dto"
	"gitee.com/song-yao-shun/MQH/model"
	"gitee.com/song-yao-shun/MQH/response"
	"gitee.com/song-yao-shun/MQH/respository"
	"gitee.com/song-yao-shun/MQH/vo"
	"github.com/gin-gonic/gin"
	"strconv"
)

type IPageController interface {
	RestController
}

type PageController struct {
	PageRty respository.PageRepository
}

func (p PageController) Create(ctx *gin.Context) {
	// 接收参数
	var requestPage vo.CreatePageRequest
	if err := ctx.ShouldBind(&requestPage); err != nil {
		response.Fail(ctx, nil, "数据验证错误！")
		return
	}

	a := respository.NewApplicationRepository()
	_, err := a.SelectById(requestPage.ApplicationId)
	if err != nil {
		response.Fail(ctx, nil, "应用不存在！")
		return
	}

	// 创建 page
	page := model.Page{
		ApplicationId: 		requestPage.ApplicationId,
		Name: 				requestPage.Name,
		Img: 				requestPage.Img,
		Type: 				requestPage.Type,
		Desc: 				requestPage.Desc,
	}

	pageId, err := p.PageRty.Create(page)
	if err != nil {
		response.Fail(ctx, nil, "新建失败！")
		return
	}

	// 返回
	response.Success(ctx, gin.H{"data":pageId}, "新建成功！")
}

func (p PageController) Update(ctx *gin.Context) {
	// 接收参数
	var requestPage vo.CreatePageRequest
	if err := ctx.ShouldBind(&requestPage); err != nil {
		response.Fail(ctx, nil, "数据验证错误，请检查提交的内容！")
		return
	}

	// 获取 path 中的参数
	pageId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	updatePage, err := p.PageRty.SelectById(pageId)
	if err != nil {
		response.Fail(ctx, nil, "页面不存在！")
		return
	}

	// 判断 appid 是否有效
	if updatePage.ApplicationId != requestPage.ApplicationId{
		a := respository.NewApplicationRepository()
		_, err := a.SelectById(requestPage.ApplicationId)
		if err != nil {
			response.Fail(ctx, nil, "修改后的应用不存在！")
			return
		}
	}

	// 更新 struct
	updatePage.ApplicationId 	= requestPage.ApplicationId
	updatePage.Name		 		= requestPage.Name
	updatePage.Img 				= requestPage.Img
	updatePage.Type 			= requestPage.Type
	updatePage.Desc 			= requestPage.Desc


	page, err := p.PageRty.Update(*updatePage)
	if err != nil {
		panic(err)
	}

	response.Success(ctx, gin.H{"data":page}, "修改成功。")
}

func (p PageController) Show(ctx *gin.Context) {
	// 获取 path 中的参数
	pageId, _ := strconv.Atoi(ctx.Params.ByName("id"))
	page := p.PageRty.SelectFullPageById(pageId)
	if page.ID == 0 {
		response.Fail(ctx, nil, "页面不存在！")
		return
	}

	response.Success(ctx, gin.H{"data":page}, "")
}

func (p PageController) Delete(ctx *gin.Context) {
	// 获取 path 中的参数
	pageId, _ := strconv.Atoi(ctx.Params.ByName("id"))

	// 判断删除的id是否为部分fct的外键
	var total int64
	f := respository.NewFctRepository()
	f.DB.Model(&model.Fct{}).Where("page_id = ?",pageId).Count(&total)
	if total != 0 {
		response.Fail(ctx, nil, "删除失败，该 页面下 存在功能")
		return
	}

	if rowsAffected := p.PageRty.DeleteById(pageId);rowsAffected != 1 {
		response.Fail(ctx, nil, "删除失败，请重试！")
		return
	}

	response.Success(ctx, nil, "删除成功！")
}

func (p PageController) List(ctx *gin.Context) {
	// 接收参数
	var pageList vo.PageList
	if err := ctx.ShouldBind(&pageList); err != nil {
		response.Fail(ctx, nil, "数据验证错误，请检查提交的内容！")
		return
	}

	fullPages, total := p.PageRty.List(pageList)

	if pageList.SelectType != ""{
		response.Success(ctx, gin.H{"data":dto.ToPageDto(fullPages), "total":total}, "查询列表成功。")
		return
	}

	response.Success(ctx, gin.H{"data":fullPages, "total":total}, "查询列表成功。")
}

func NewPageController() IPageController{
	return PageController{PageRty: respository.NewPageRepository()}
}
